package com.czxy.service.impl;

import com.czxy.dao.CertificateTypeDao;
import com.czxy.dao.UserDao;
import com.czxy.domain.CertificateType;
import com.czxy.domain.User;
import com.czxy.domain.UserVo;
import com.czxy.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhangmengxiang1@itcast.cn
 * @version 1.0
 * @date 2020/1/30
 */
@Service
public class UserServiceimpl implements UserService {
    @Resource
    private UserDao userDao;

    @Resource
    private CertificateTypeDao certificateTypeDao;

    @Override
    public List<User> findAllUser() {
        return userDao.selectAll();
    }

    @Override
    public List<CertificateType> findAllidType() {
        return certificateTypeDao.selectAll();
    }

    @Override
    public void addUser(User user) {
        user.setState(1);
        userDao.insert(user);
    }

    @Override
    public User login(User user) {
        Example example = new Example(User.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("uname",user.getUname());
        c.andEqualTo("upassword",user.getUpassword());
        User user1 = userDao.selectOneByExample(example);
        return user1;
    }

    @Override
    public void updateuser(User user) {
        userDao.updateByPrimaryKeySelective(user);
    }

    @Override
    public void delUserByuid(Integer uid) {
        userDao.deleteByPrimaryKey(uid);
    }

    @Override
    public void delUserByStr(String dir) {
        String[] split = dir.split(",");
        for (String s : split) {
            userDao.deleteByPrimaryKey(Integer.parseInt(s));
        }
    }

    @Override
    public PageInfo<User> findAllUserForpage(UserVo userVo) {
        PageHelper.startPage(userVo.getPageNum(),userVo.getPageSize());
        List<User> userList = userDao.findAllUserForpage(userVo);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    @Override
    public User findOneUserByuid(Integer uid) {
        User user = userDao.findOneUserByuid(uid);
        return user;
    }
}
